home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / ByteArrayInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  8.7 KB  |  278 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)ByteArrayInputStream.java    1.32 98/09/30
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A <code>ByteArrayInputStream</code> contains
  19.  * an internal buffer that contains bytes that
  20.  * may be read from the stream. An internal
  21.  * counter keeps track of the next byte to
  22.  * be supplied by the <code>read</code> method.
  23.  *
  24.  * @author  Arthur van Hoff
  25.  * @version 1.32, 09/30/98
  26.  * @see     java.io.StringBufferInputStream
  27.  * @since   JDK1.0
  28.  */
  29. public
  30. class ByteArrayInputStream extends InputStream {
  31.  
  32.     /**
  33.      * A flag that is set to true when this stream is closed.
  34.      */
  35.     private boolean isClosed = false;
  36.  
  37.     /**
  38.      * An array of bytes that was provided
  39.      * by the creator of the stream. Elements <code>buf[0]</code>
  40.      * through <code>buf[count-1]</code> are the
  41.      * only bytes that can ever be read from the
  42.      * stream;  element <code>buf[pos]</code> is
  43.      * the next byte to be read.
  44.      */
  45.     protected byte buf[];
  46.  
  47.     /**
  48.      * The index of the next character to read from the input stream buffer.
  49.      * This value should always be nonnegative
  50.      * and not larger than the value of <code>count</code>.
  51.      * The next byte to be read from the input stream buffer 
  52.      * will be <code>buf[pos]</code>.
  53.      */
  54.     protected int pos;
  55.  
  56.     /**
  57.      * The currently marked position in the stream.
  58.      * ByteArrayInputStream objects are marked at position zero by
  59.      * default when constructed.  They may be marked at another
  60.      * position within the buffer by the <code>mark()</code> method.
  61.      * The current buffer position is set to this point by the
  62.      * <code>reset()</code> method.
  63.      *
  64.      * @since   JDK1.1
  65.      */
  66.     protected int mark = 0;
  67.  
  68.     /**
  69.      * The index one greater than the last valid character in the input 
  70.      * stream buffer. 
  71.      * This value should always be nonnegative
  72.      * and not larger than the length of <code>buf</code>.
  73.      * It  is one greater than the position of
  74.      * the last byte within <code>buf</code> that
  75.      * can ever be read  from the input stream buffer.
  76.      */
  77.     protected int count;
  78.  
  79.     /**
  80.      * Creates a <code>ByteArrayInputStream</code>
  81.      * so that it  uses <code>buf</code> as its
  82.      * buffer array. 
  83.      * The buffer array is not copied. 
  84.      * The initial value of <code>pos</code>
  85.      * is <code>0</code> and the initial value
  86.      * of  <code>count</code> is the length of
  87.      * <code>buf</code>.
  88.      *
  89.      * @param   buf   the input buffer.
  90.      */
  91.     public ByteArrayInputStream(byte buf[]) {
  92.     this.buf = buf;
  93.         this.pos = 0;
  94.     this.count = buf.length;
  95.     }
  96.  
  97.     /**
  98.      * Creates <code>ByteArrayInputStream</code>
  99.      * that uses <code>buf</code> as its
  100.      * buffer array. The initial value of <code>pos</code>
  101.      * is <code>offset</code> and the initial value
  102.      * of <code>count</code> is <code>offset+len</code>.
  103.      * The buffer array is not copied. 
  104.      * <p>
  105.      * Note that if bytes are simply read from
  106.      * the resulting input stream, elements <code>buf[pos]</code>
  107.      * through <code>buf[pos+len-1]</code> will
  108.      * be read; however, if a <code>reset</code>
  109.      * operation  is performed, then bytes <code>buf[0]</code>
  110.      * through b<code>uf[pos-1]</code> will then
  111.      * become available for input.
  112.      *
  113.      * @param   buf      the input buffer.
  114.      * @param   offset   the offset in the buffer of the first byte to read.
  115.      * @param   length   the maximum number of bytes to read from the buffer.
  116.      */
  117.     public ByteArrayInputStream(byte buf[], int offset, int length) {
  118.     this.buf = buf;
  119.         this.pos = offset;
  120.     this.count = Math.min(offset + length, buf.length);
  121.         this.mark = offset;
  122.     }
  123.  
  124.     /**
  125.      * Reads the next byte of data from this input stream. The value 
  126.      * byte is returned as an <code>int</code> in the range 
  127.      * <code>0</code> to <code>255</code>. If no byte is available 
  128.      * because the end of the stream has been reached, the value 
  129.      * <code>-1</code> is returned. 
  130.      * <p>
  131.      * This <code>read</code> method 
  132.      * cannot block. 
  133.      *
  134.      * @return  the next byte of data, or <code>-1</code> if the end of the
  135.      *          stream has been reached.
  136.      */
  137.     public synchronized int read() {
  138.     ensureOpen();
  139.     return (pos < count) ? (buf[pos++] & 0xff) : -1;
  140.     }
  141.  
  142.     /**
  143.      * Reads up to <code>len</code> bytes of data into an array of bytes 
  144.      * from this input stream. 
  145.      * If <code>pos</code> equals <code>count</code>,
  146.      * then <code>-1</code> is returned to indicate
  147.      * end of file. Otherwise, the  number <code>k</code>
  148.      * of bytes read is equal to the smaller of
  149.      * <code>len</code> and <code>count-pos</code>.
  150.      * If <code>k</code> is positive, then bytes
  151.      * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
  152.      * are copied into <code>b[off]</code>  through
  153.      * <code>b[off+k-1]</code> in the manner performed
  154.      * by <code>System.arraycopy</code>. The
  155.      * value <code>k</code> is added into <code>pos</code>
  156.      * and <code>k</code> is returned.
  157.      * <p>
  158.      * This <code>read</code> method cannot block. 
  159.      *
  160.      * @param   b     the buffer into which the data is read.
  161.      * @param   off   the start offset of the data.
  162.      * @param   len   the maximum number of bytes read.
  163.      * @return  the total number of bytes read into the buffer, or
  164.      *          <code>-1</code> if there is no more data because the end of
  165.      *          the stream has been reached.
  166.      */
  167.     public synchronized int read(byte b[], int off, int len) {
  168.     ensureOpen();
  169.     if (b == null) {
  170.         throw new NullPointerException();
  171.     } else if ((off < 0) || (off > b.length) || (len < 0) ||
  172.            ((off + len) > b.length) || ((off + len) < 0)) {
  173.         throw new IndexOutOfBoundsException();
  174.     }
  175.     if (pos >= count) {
  176.         return -1;
  177.     }
  178.     if (pos + len > count) {
  179.         len = count - pos;
  180.     }
  181.     if (len <= 0) {
  182.         return 0;
  183.     }
  184.     System.arraycopy(buf, pos, b, off, len);
  185.     pos += len;
  186.     return len;
  187.     }
  188.  
  189.     /**
  190.      * Skips <code>n</code> bytes of input from this input stream. Fewer 
  191.      * bytes might be skipped if the end of the input stream is reached. 
  192.      * The actual number <code>k</code>
  193.      * of bytes to be skipped is equal to the smaller
  194.      * of <code>n</code> and  <code>count-pos</code>.
  195.      * The value <code>k</code> is added into <code>pos</code>
  196.      * and <code>k</code> is returned.
  197.      *
  198.      * @param   n   the number of bytes to be skipped.
  199.      * @return  the actual number of bytes skipped.
  200.      */
  201.     public synchronized long skip(long n) {
  202.     ensureOpen();
  203.     if (pos + n > count) {
  204.         n = count - pos;
  205.     }
  206.     if (n < 0) {
  207.         return 0;
  208.     }
  209.     pos += n;
  210.     return n;
  211.     }
  212.  
  213.     /**
  214.      * Returns the number of bytes that can be read from this input 
  215.      * stream without blocking. 
  216.      * The value returned is
  217.      * <code>count - pos</code>, 
  218.      * which is the number of bytes remaining to be read from the input buffer.
  219.      *
  220.      * @return  the number of bytes that can be read from the input stream
  221.      *          without blocking.
  222.      */
  223.     public synchronized int available() {
  224.     ensureOpen();
  225.     return count - pos;
  226.     }
  227.  
  228.     /**
  229.      * Tests if ByteArrayInputStream supports mark/reset.
  230.      *
  231.      * @since   JDK1.1
  232.      */
  233.     public boolean markSupported() {
  234.     return true;
  235.     }
  236.  
  237.     /**
  238.      * Set the current marked position in the stream.
  239.      * ByteArrayInputStream objects are marked at position zero by
  240.      * default when constructed.  They may be marked at another
  241.      * position within the buffer by this method.
  242.      *
  243.      * @since   JDK1.1
  244.      */
  245.     public void mark(int readAheadLimit) {
  246.     ensureOpen();
  247.     mark = pos;
  248.     }
  249.  
  250.     /**
  251.      * Resets the buffer to the marked position.  The marked position
  252.      * is the beginning unless another position was marked.
  253.      * The value of </code>pos</code> is set to 0.
  254.      */
  255.     public synchronized void reset() {
  256.     ensureOpen();
  257.     pos = mark;
  258.     }
  259.  
  260.     /**
  261.      * Closes this input stream and releases any system resources 
  262.      * associated with the stream. 
  263.      * <p>
  264.      */
  265.     public synchronized void close() throws IOException {
  266.     isClosed = true;
  267.     }
  268.  
  269.     /** Check to make sure that the stream has not been closed */
  270.     private void ensureOpen() {
  271.         /* This method does nothing for now.  Once we add throws clauses
  272.      * to the I/O methods in this class, it will throw an IOException
  273.      * if the stream has been closed.
  274.      */
  275.     }
  276.  
  277. }
  278.